# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=nomad-driver-nspawn
pkgver=0.10.0
pkgrel=1
pkgdesc='A nomad taskdriver for systemd-nspawn'
arch=('loong64' 'x86_64')
url='https://github.com/JanMa/nomad-driver-nspawn'
license=('MIT')
depends=('glibc' 'nomad' 'systemd')
makedepends=('git' 'go')
options=('!lto')
_commit='3d7877c1212ff3f340e0e9c42c82357b7fd31444'
source=("$pkgname::git+$url#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
	cd "$pkgname"

  # create directory for build output
  mkdir build

  # verify modules
  go mod verify
}

build() {
	cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=vendor \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS} \
    -X "github.com/JanMa/nomad-driver-nspawn/nspawn.pluginVersion=${pkgver}"" \
    -o build \
    .
}

package() {
	cd "$pkgname"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md
  cp -r example "$pkgdir/usr/share/doc/$pkgname"

  # plugin
  install -vDm755 -t "$pkgdir/usr/lib/nomad/plugins" "build/$pkgname"

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
